package primalgorithms.dynamicplanning;

public class _03MaxSubArray {
    public static int maxSubArray(int[] nums) {
        //贪心策略 ==> 求最大子序列的和
        int nowMax = nums[0],finMax = nowMax;
        for (int i = 1;i < nums.length;i++){
            //当先max如果是负数，应该直接舍弃，考虑到下一个数也可能是负数，所以选择取两者较大的
            nowMax = nowMax < 0 ? Math.max(nowMax,nums[i]) : nowMax + nums[i];
            finMax = Math.max(nowMax,finMax);
        }
        return finMax;
    }
}
